package com.bimbimbip.explorer;

import android.content.Context;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {

	FileItem[] items;
	int count = 0;

	private LayoutInflater mLayoutInflater;
//	private Context mContext;

	public ImageAdapter(Context c, FileItem[] items) {
//		mContext = c;
		this.items = items;
		mLayoutInflater = LayoutInflater.from(c);
		count = items.length;
	}

	public int getCount() {
		return count;
	}

	public Object getItem(int position) {
		return items[position];
	}

	public long getItemId(int position) {
		return position;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewFile mVHolder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.view_file, parent,
					false);
			mVHolder = new ViewFile();
			mVHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			mVHolder.fileName = (TextView) convertView.findViewById(R.id.text01);
			mVHolder.icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
			mVHolder.icon.setPadding(5, 5, 5, 5);
			convertView.setTag(mVHolder);
		} else {
			mVHolder = (ViewFile) convertView.getTag();
		}
		
		mVHolder.setFileName(cutString(items[position].display));
		Log.v("FILE NAME>>>>>>>>>>>>", items[position].display);
		mVHolder.setFilePath(items[position].path);

		if (items[position].tag != null)
			mVHolder.icon.setImageResource(IconStore.getIconByExtendsion(items[position].tag));
		else if (items[position].isFile) 
			mVHolder.icon.setImageResource(IconStore.getIconByExtendsion(items[position].path));
		else
			mVHolder.icon.setImageResource(R.drawable.folder);
		Log.v("bimbimbip", "items" + position);
		Log.v("bimbimbip", "Path = " + items[position].path);
		Log.d("bimbimbip", "IconStore::getIconByExtendsion");
		return convertView;
	}

	public String cutString(String name) {

		if (name.length() > 7) {
			name = name.substring(0, 7) + "...";
		}
		return name;
	}
}